package cvr100demo;/*
 * CVR100DemoView.java
 */

import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;


/**
 * The application's main frame.
 */
public class CVR100DemoView {

    String strTmp = "";
    int len = 0;
    int ret = 0;

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    public int CVR_InitComm(int Port) throws NativeException, IllegalAccessException, UnsupportedEncodingException {
        JNative n = null;
        try {
            n = new JNative("Termb.dll", "CVR_InitComm");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.setParameter(0, Port);
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetPeopleName() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative("Termb.dll", "GetPeopleName");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            byte[] bt = new byte[40];
            bt = a.getMemory();
            len = a.getAsInt(0);
            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetPeopleAddress() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative("Termb.dll", "GetPeopleAddress");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            len = a.getAsInt(0);

            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetPeopleIDCode() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative("Termb.dll", "GetPeopleIDCode");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            len = a.getAsInt(0);

            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int CVR_Authenticate() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative("Termb.dll", "CVR_Authenticate");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int CVR_Read_Content(int Active) throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative("Termb.dll", "CVR_Read_Content");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.setParameter(0, Active);
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int CVR_CloseComm() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative("Termb.dll", "CVR_CloseComm");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
// TODO add your handling code here:  
        String strport = "";
        int intport = 0;
        strport = strport.replaceAll("USB", "");
        intport = Integer.parseInt(strport);
        int ret = 0;
        try {
            ret = CVR_InitComm(intport);
            System.out.println("返回值：" + String.valueOf(ret));
        } catch (Exception ex) {
            System.out.println("调用异常！" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton1MouseClicked

    public void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        int ret = 0;
        try {
            ret = CVR_Authenticate();
            System.out.println("返回值：" + String.valueOf(ret));
        } catch (Exception ex) {
            System.out.println("调用异常！" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton2MouseClicked

    public void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        // TODO add your handling code here:
        int ret = 0;
        try {
            ret = CVR_Read_Content(4);
            System.out.println("返回值：" + String.valueOf(ret));
        } catch (Exception ex) {
            System.out.println("调用异常！" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton3MouseClicked

    public void jButton8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton8MouseClicked
        // TODO add your handling code here:
        int ret = 0;
        try {
            ret = CVR_CloseComm();
            System.out.println("返回值：" + String.valueOf(ret));
        } catch (Exception ex) {
            System.out.println("调用异常！" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton8MouseClicked

    public void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
        // TODO add your handling code here:       
        try {
            ret = GetPeopleName();
            System.out.println("返回值：" + String.valueOf(ret));
            if (ret == 1) {
                System.out.println("姓名：" + strTmp.trim());
            }
        } catch (Exception ex) {
            System.out.println("调用异常！" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton4MouseClicked

    public void jButton6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton6MouseClicked
        // TODO add your handling code here:
        try {
            ret = GetPeopleAddress();
            System.out.println("返回值：" + String.valueOf(ret));
            if (ret == 1) {
                System.out.println("地址：" + strTmp.trim());
            }
        } catch (Exception ex) {
            System.out.println(("调用异常！" + ex.getMessage()));
        }
    }//GEN-LAST:event_jButton6MouseClicked

    public void jButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked
        // TODO add your handling code here:
        try {
            ret = GetPeopleIDCode();
            System.out.println(("返回值：" + String.valueOf(ret)));
            if (ret == 1) {
                System.out.println("身份号码：" + strTmp.trim());
            }
        } catch (Exception ex) {
            System.out.println("调用异常！" + ex.getMessage());
        }
    }//GEN-LAST:event_jButton5MouseClicked

}
